package com.songlanyun.modules.rabbitmq.mq.consumer;

import com.rabbitmq.client.Channel;
import com.songlanyun.common.utils.custom.ClassConvertUtils;
import com.songlanyun.modules.order.entity.ChildOrder;
import com.songlanyun.modules.order.service.ChildOrderService;
import com.songlanyun.modules.rabbitmq.DelayedConstant;
import com.songlanyun.modules.saleconfig.entity.SaleConfig;
import com.songlanyun.modules.saleconfig.service.SaleConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 自动评论
 */
@Component
@Slf4j
public class AutoCommentChildOrderConsumer extends BaseConsumer {

    @Autowired
    private ChildOrderService childOrderService;

    @Autowired
    private SaleConfigService saleConfigService;

    @Override
    protected void process(Message message) {
        Long childId = ClassConvertUtils.objToLong(new String(message.getBody()));
        SaleConfig config = saleConfigService.getConfig();
        ChildOrder v = childOrderService.getById(childId);
        if(config!=null && config.getCommentOpen())
        {
            try {
                log.info(String.format("订单:%s,购买商品【%s】自动评论开始", v.getOrderCode(),v.getGoodsTitle()));
                childOrderService.autoCommentChildOrder(v);
                log.info(String.format("订单:%s,购买商品【%s】自动评论完成", v.getOrderCode(),v.getGoodsTitle()));
            }
            catch (Exception ex) {
                log.error(String.format("订单:%s,购买商品【%s】自动评论,出错%s", v.getOrderCode(),v.getGoodsTitle(), ex.getMessage()));
                throw ex;
            }
        }
    }

    @RabbitListener(queues = DelayedConstant.ChildOrderAutoCommentConstant.CHILD_ORDER_AUTO_COMMENT_DELAYED_QUEUE)
    public void consume(Message message, Channel channel) throws IOException {
        super.handle(message,channel);
    }
}
